import {computed, getCurrentInstance, reactive, ref} from "vue";
import {getAllRole} from "@/api/system/role";
import {DictData} from "@/hooks/useTranslateDictCode/useTranslateDictCode";
import {UserSearchForm} from "@/api/system/user";
import useTable from "@/views/system/user/hooks/useTable";
import {Role} from "@/views/system/role/hooks/useTable"
import moment from "moment";
import mittEvent,{ROLE_READY_EVENT}from "@/utils/mittEvent";
export const searchForm = reactive<UserSearchForm>({
    username:"",
    phone:"",
    createTimeRange:[],
    updateTimeRange:[],
    roleCodes:[],
    deptId:undefined,
    page:1,
    pageSize:10
})
export const roleList = ref<Role[]>([]);
export default function useSearch() {
    const instance = getCurrentInstance()
    const {getUserDataList} = useTable()
    function getRoleBuildDict() {
        getAllRole().then(res=>{
            mittEvent.emit(ROLE_READY_EVENT)
            console.log("getallRole")
            roleList.value = res.data
        })
    }
    function handleSearch() {
        searchForm.createTimeRange = searchForm.createTimeRange?.map(d=>{
            return moment(d).format("YYYY-MM-DD")
        })
        searchForm.updateTimeRange = searchForm.updateTimeRange?.map(d=>{
            return moment(d).format("YYYY-MM-DD")
        })
        getUserDataList()
    }
    function resetSearchForm() {
        //eslint-disable-next-line
        (instance?.refs.searchFormRef as any).resetFields()
        getUserDataList()
    }
    const  roleDict = computed<DictData[]>(()=>{
      return    roleList.value.map(d=>{
            return {
                label:d.roleDesc,
                value:d.roleName
            }
        })
    })
    return {
        searchForm,
        getRoleBuildDict,
        roleDict,
        handleSearch,
        resetSearchForm
    }
}
